<?php
	class DAO
	{
		private $socket;
		
		private function connect()
		{
                        $host = "137.219.47.188";
                        $port = 6881;

                        // produce a fatal error if the connection is not made after 10 seconds
                        //set_time_limit(10);
	
			// create socket
			$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Cound not create connection socket!");

                        // bind socket to a port
                        $bindResult = socket_connect($this->socket, $host, $port) or die("Could not connect to the server!");
		}

	
		private function disconnect()
		{
			socket_close($this->socket);
		}
	
	
		// dbQuery returns an array of results (This is for "SELECT" sql queries)
		public function sendRequest($xmlRequest)
		{
			   $host = "137.219.47.188";
                        $port = 6881;

                        // produce a fatal error if the connection is not made after 10 seconds
                        //set_time_limit(10);

			// create socket
			$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Cound not create connection socket!");

                        // bind socket to a port
                        $bindResult = socket_connect($this->socket, $host, $port) or die("Could not connect to the server!");

                        // send the request through the socket
			$bytesSent = socket_write($this->socket, $xmlRequest);

                        // shut down the output stream of the socket (similar to flush)
                        socket_shutdown($this->socket, 1);
                        
                        $response = "";
                        do
                        {
                            $recv = "";
                            $recv = socket_read($this->socket, 2000);

                            if ($recv != "")
                            {
                                // append the response fragment to the main response
                                $response .= $recv;
                            }

                        } while ($recv != "");


			// Close database connection
			$this->disconnect();

                        return $response;
		}
	}
?>
